Ext.onReady(function() {
	Ext.SSL_SECURE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
	Ext.BLANK_IMAGE_URL = "../../ext-3.2.1/resources/images/default/s.gif";
	// Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	//Ext.util.CSS.swapStyleSheet('theme', currentTheme);
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'side';
	var pageSize = 20;

	// tab页面
	var centerTabPanel = new Ext.TabPanel({
		region : 'center',
		deferredRender : false,
		activeTab : 1,
		margins : '0 5 0 0',
		id : 'centerTabPanelPro',
		// plugins : new Ext.ux.TabCloseMenu(),
		resizeTabs : false,
		minTabWidth : 50,
		// tabWidth : 230,

		enableTabScroll : true,
		autoScroll : true,
		items : [{
			title : '下级标签：',
			// tabWidth:120,
			iconCls : 'tabsDown',
			// contentEl:'grid-div',
			disabled : true
				// autoScroll:true
				}, {
					title : '组信息维护',
					iconCls : 'tabs',
					// tabWidth:150,
					contentEl : 'grid-div',
					autoScroll : true
				}],
		loadPage : function(href, cls, title) {
			var tabId = 'workPanel' + cls;
			var tab = this.getComponent(tabId);
			if (tab) {
				this.setActiveTab(tab);
			} else {
				var autoLoad = {
					url : href,
					scripts : true
				};
				var p = this.add(new DocPanel({
					id : tabId,
					cclass : cls,
					layout : 'fit',
					items : [new IFrameComponent({
						id : tabId,
						url : href
					})],
					title : title,
					iconCls : 'tabs'
				}));
				this.setActiveTab(p);
			}
		}
	});
	IFrameComponent = Ext.extend(Ext.BoxComponent, {
		onRender : function(ct, position) {
			this.el = ct.createChild({
				tag : 'iframe',
				id : 'iframe-' + this.id,
				frameBorder : 0,
				src : this.url
			});
		}
	});
	DocPanel = Ext.extend(Ext.Panel, {
		closable : true,
		autoScroll : true,
		initComponent : function() {
			DocPanel.superclass.initComponent.call(this);
		},
		scrollToMember : function(member) {
			var el = Ext.fly(this.cclass + '-' + member);
			if (el) {
				var top = (el.getOffsetsTo(this.body)[1])
						+ this.body.dom.scrollTop;
				this.body.scrollTo('top', top - 25, {
					duration : .75,
					callback : this.hlMember.createDelegate(this, [member])
				});
			}
		},
		scrollToSection : function(id) {
			var el = Ext.getDom(id);
			if (el) {
				var top = (Ext.fly(el).getOffsetsTo(this.body)[1])
						+ this.body.dom.scrollTop;
				this.body.scrollTo('top', top - 25, {
					duration : .5,
					callback : function() {
						Ext.fly(el).next('h2').pause(.2).highlight('#8DB2E3', {
							attr : 'color'
						});
					}
				});
			}
		},
		hlMember : function(member) {
			var el = Ext.fly(this.cclass + '-' + member);
			if (el) {
				el.up('tr').highlight('#cadaf9');
			}
		}
	});

	new Ext.Viewport({
		region : 'center',
		layout : 'fit',
		items : centerTabPanel
	});

	var queryButton = new Ext.Toolbar.Button({
		icon : "../../ext-3.2.1/examples/shared/icons/fam/query.png",
		id : 'queryButton',
		cls : 'x-btn-text-icon',
		text : '查询',
		tooltip : '查询记录',
		handler : onQueryClick
	});
	var groupName = new Ext.form.TextField({
		fieldLabel : '组名称',
		id : 'groupName',
		name : 'groupName',
		width : 300,
		maxLength : 50,
		maxLengthText : "最大长度50"
	});
	var addButton = new Ext.Toolbar.Button({
		icon : "../../ext-3.2.1/examples/shared/icons/fam/add.png",
		id : 'addButton',
		cls : 'x-btn-text-icon',
		text : '添加',
		// disabled:true,
		tooltip : '添加一条新的记录',
		handler : onAddClick
	});
	var modButton = new Ext.Toolbar.Button({
		icon : "../../ext-3.2.1/examples/shared/icons/fam/edit.png",
		id : 'modButton',
		cls : 'x-btn-text-icon',
		text : '修改',
		//disabled : true,
		tooltip : '修改一条选中记录。',
		handler : onModClick
	});
	var delButton = new Ext.Toolbar.Button({
		icon : "../../ext-3.2.1/examples/shared/icons/fam/del.png",
		id : 'delButton',
		cls : 'x-btn-text-icon',
		text : '删除',
		//disabled : true,
		tooltip : '删除已选中的记录。',
		handler : onDelClick
	});

	var viewButton = new Ext.Toolbar.Button({
		icon : "../../ext-3.2.1/examples/shared/icons/fam/manage.png",
		id : 'viewButton',
		cls : 'x-btn-text-icon',
		text : '浏览',
		//disabled : false,
		tooltip : '浏览一条选中记录。',
		handler : onViewClick
	});

	
	//列表中按钮的方法
	function createGridButton(index, value, contentid, record) {
		new Ext.Button({
			renderTo : contentid,
			text : value,
			handler : function(btn, e) {
						var ridStr = record.id;
						centerTabPanel.loadPage('doctorList.jsp?hid='+hid+'&did='+did+'&gid='+ridStr, ridStr, value);
					},
			cls : 'x-btn-text',
			scope : this
		});
	}

	function renderBtn_inputDepartment(val, p, record) {
		var contentId = Ext.id();
		createGridButton.defer(1, this, [1, "医生设定", contentId, record]);
		return ('<div id="' + contentId + '"></div>');
	}

	//列表数据仓库
	var gridStore = new Ext.data.Store({
		proxy : new Ext.data.ScriptTagProxy({
			url : '../../group/buildJSONString.action?hid='+hid+'&did='+did
		}),
		reader : new Ext.data.JsonReader({
			root : 'results',
			totalProperty : 'total',
			id : 'id'
		}, [{
			name : 'id',
			mapping : 'id'
		}, {
			name : 'hid',
			mapping : 'hid'
		}, {
			name : 'did',
			mapping : 'did'
		}, {
			name : 'name',
			mapping : 'name'
		}, {
			name : 'description',
			mapping : 'description'
		}, {
			name : 'deleteFlag',
			mapping : 'deleteFlag'
		}, {
			name : 'inputDate',
			mapping : 'inputDate'
		}, {
			name : 'inputMan',
			mapping : 'inputMan'
		}, {
			name : 'inputManId',
			mapping : 'inputManId'
		}
		]),
		remoteSort : false
	});

	var sm = new Ext.grid.CheckboxSelectionModel();

	// var cm = new Ext.grid.LockingColumnModel([sm,{
	var cm = new Ext.ux.grid.LockingColumnModel([sm, {
		id : 'id',
		header : "主键",
		dataIndex : 'id',
		width : 60,
		sortable : true,
		hidden : true,
		css : 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
	}, {
		id : 'name',
		header : "组名称",
		dataIndex : 'name',
		width : 200,
		align : 'center',
		hidden : false,
		sortable : false,
		css : 'white-space:normal;border:#eee solid;border-width:0 0 0 1;vertical-align:middle;'
	}, {
		id : 'inputDate',
		header : "创建时间",
		dataIndex : 'inputDate',
		width : 150,
		sortable : false,
		align : 'center',
		css : 'white-space:normal;border:#eee solid;border-width:0 0 0 1;vertical-align:middle;'
	}, {
		id : 'inputMan',
		header : "创建人",
		dataIndex : 'inputMan',
		width : 100,
		align : 'center',
		sortable : true,
		hidden : false,
		css : 'white-space:normal;border:#eee solid;border-width:0 0 0 1;height:30;vertical-align:middle;'
	}, {
		id : '',
		header : "医生维护",
		dataIndex : '',
		align : 'center',
		width : 100,
		sortable : false,
		renderer : renderBtn_inputDepartment,
		css : 'white-space:normal;border:#eee solid;border-width:0 0 0 1;'
	}
	]);

	// var grid = new Ext.grid.LockingGridPanel({
	var grid = new Ext.ux.grid.SSNJLOCKPAGEGrid({
		// el:'grid-div',
		height : gridHeight,
		id : 'grid',
		layout : 'fit',
		store : gridStore,
		border : false,
		loadMask : {
			msg : '正在加载数据，请稍候……'
		},
		cm : cm,
		sm : sm,
		viewConfig : {
			enableRowBody : false
		// forceFit:true
		},
		bodyStyle : 'width:100%;',
		tbar : ['组名称：',groupName,'-',queryButton, '-',
				viewButton, '-', addButton, '-', modButton, '-', delButton, '-'],
		frame : false,
		stripeRows : true
	});

	// grid.render();
	grid.render(Ext.get('grid-div'));
    gridStore.load({params:{start:0, limit:pageSize}});

	
	function onAddClick(item) {
		centerTabPanel.loadPage(encodeURI("../../group/showPage.action?hid="+hid+'&did='+did+"&flag=1"),'add', "组添加" );
	}
	
	function onModClick(item) {
		var selectedCount = sm.getCount();
		if (selectedCount == 0) {
			Ext.Msg.alert('修改记录', '未选中任何一条记录，请选择要修改的记录！');
			return;
		} else if (selectedCount > 1) {
			Ext.Msg.alert('修改记录', '只能选择一条记录，不能同时选择多条记录！');
			return;
		}
		record = sm.getSelected();
		id = record.id;
		centerTabPanel.loadPage(encodeURI("../../group/showPage.action?hid="+hid+"&did="+did+"&gid="+ id+"&flag=1"), 'modify', "组修改");
	}
	function onDelClick(item) {
		var selectedCount = sm.getCount();
		if (selectedCount == 0) {
			Ext.Msg.alert('删除操作', '未选中要删除的数据，请选择要删除的数据！');
			return;
		} else {
			var records = sm.getSelections();
			Ext.Msg.confirm('删除记录', '请确认要删除记录吗?按是将删除选中记录！', function(btn) {
				if (btn == 'yes') {
					var records = sm.getSelections();
					var _ids = '';
					for (var i = 0; i < records.length; i++) {
						_ids += records[i].id;
						_ids += '_';
					}
					Ext.MessageBox.wait('正在处理，请稍等...');
					Ext.Ajax.request({
						url : '../../group/delete.action',
						failure : function(result, request) {
							Ext.MessageBox.hide();
							Ext.Msg.show({
								title : '错误',
								msg : '<span style="text-align:center;width:250px;margin:5 0 5 0;">'
										+ '删除失败！' + '</span>',
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.WARNING
							});
						},
						success : function(result, request) {
							Ext.MessageBox.hide();
							Ext.Msg.show({
								title : '成功',
								msg : '<span style="text-align:center;width:250px;margin:5 0 5 0;">'
										+ '删除成功！' + '</span>',
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.INFO
							});
							gridStore.reload();
						},
						params : {
							deleteID : _ids
						}
					});
				}
			});
		}
	}
	function onViewClick() {
		var selectedCount = sm.getCount();
		if (selectedCount == 0) {
			Ext.Msg.alert('浏览记录', '未选中任何一条记录，请选择要浏览的记录！');
			return;
		} else if (selectedCount > 1) {
			Ext.Msg.alert('浏览记录', '只能选择一条记录，不能同时选择多条记录！');
			return;
		}
		record = sm.getSelected();
		id = record.id;
		centerTabPanel.loadPage(encodeURI("../../group/showPage.action?hid="+hid+"&did="+did+"&gid="+ id+"&flag=0"), 'view', "组信息浏览");
	}
	
		
	function onQueryClick(item) {
		gridStore.proxy = new Ext.data.ScriptTagProxy({
			url : encodeURI('../../group/buildJSONString.action?hid='+hid+'&did='+did+'&groupName='
					+ groupName.getValue())
		});
		gridStore.reload({
			params : {
				start : 0,
				limit : pageSize
			}
		});
	}
})